<?php
/*
 +----------------------------------------------------------------------
 | TEMMOKUMVC [ NO BEST, ONLY BETTER ]
 +----------------------------------------------------------------------
 | Copyright (c) 2018~2019 https://www.temmoku.cn All rights reserved.
 +----------------------------------------------------------------------
 | Author: 张宗强 Email:webmaster@temmoku.cn QQ:158726877 516669373 TEL:17895221001 微信:temmokumvc
 +---------------------------------------------------------------------- 
*/
namespace admin\model;
use \temmoku\db;
class setting {
   	public function setting()
    {
    	$_settingDB=db::select('*')->from(jab."setting")->query();
    	foreach($_settingDB AS $row){
    		$settingDB[$row['s_key']]=stripslashes($row['s_name']);
    	}
		return $settingDB;
    }
    
    public function post()
    {
    	foreach($_POST['web'] AS $key=>$name){
			$one=db::select('*')->from(jab."setting")->where("s_key LIKE '$key' ")->row();
			if($key=='md5' && $one['s_name'] != $name){
				$this->delconf($name,$one['s_name']);
			}
			if($one){
				db::update(jab."setting")->cols(array('s_name'))->where("s_key LIKE '$key'")->bindValue('s_name', "$name")->query();
			}else{
				db::insert(jab."setting")->cols(array('s_key'=>"$key",'s_name'=>"$name"))->query();
			}
		}
    }
	
	public function delconf($md5,$oldmd5){
		if(CACHING==true){
			define('CACHING_ALLCACHE',true);
			\temmoku\vendor\think\cache::rm('conf_file');
			\temmoku\vendor\think\cache::rm('_config');
		}
		C('MD5',$md5);
		$this->delconffile(APP_PATH.'conf');
		$this->delconffile(APP_PATH.'home'.DS.'conf');
		\admin\public_class\cache::all();
		
		//更新模块缓存
		$modular=db::select('*')->from(jab.'modular')->query();
		if($modular){
			foreach($modular AS $key=>$row){
				$this->delconffile(APP_PATH.$row['dir'].DS.'conf');
				$modular_file= APP_PATH.$row['dir']."/public_class/cache.php";
				if(is_file($modular_file)){
					//加载此文件。
					include_once $modular_file;
					//执行相关操作
					$controller=$row['dir']."\public_class\cache";
					$controller::run($row);
				}
			}
		}
		//更新插件缓存
		$plugin=db::select('*')->from(jab.'plugin')->query();
		if($plugin){
			foreach($plugin AS $key=>$row){
				$this->delconffile(APP_PATH.'plugin'.DS.$row['dir'].DS.'conf');
				$file= APP_PATH.'plugin/'.$row['dir']."/public_class/cache.php";
				if(is_file($file)){
					//加载此文件。
					include_once $file;
					//执行相关操作
					$controller='plugin\\'.$row['dir']."\public_class\cache";
					$controller::run($row);
				}
			}
		}
	}
	
	private function delconffile($path){
		$path= @realpath($path);
		if(IS_WIN=='0' && function_exists('chomd')){
			chomd($path,'0777');
		}
		if ( $handle  =  @opendir ( $path )) {
			while (( $file  =  readdir ( $handle ))!== false) {
			    if($file==='.' || $file==='..'){
			     	continue;
			    }
				$after=strtolower(strrchr($file,"."));
				if($after==='.conf'){
					\unlink($path.DS.$file);
				}elseif(is_dir($path.DS.$file)){
					$this->delconffile($path.DS.$file);
				}
			}
			//关闭打开的目录
			closedir ( $handle );
		}
	}
	
}
?>